home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 January: Technology Seed / Jan. '98 ATS.toast / NavServices1.0b3 / Navigation Services SDK / Examples / Sampler / Sampler ƒ / Utilities.c < prev   
Encoding:
C/C++ Source or Header  |  1998-01-12  |  4.0 KB  |  175 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        Utilities.c
  3.  
  4.     Copyright:    © 1997 by Apple Computer, Inc., all rights reserved.
  5.  
  6. */
  7.  
  8. #pragma segment AppSeg
  9.  
  10. #ifndef __SCRAP__
  11. #include <Scrap.h>
  12. #endif
  13.  
  14. #ifndef __TEXTEDIT__
  15. #include <TextEdit.h>
  16. #endif
  17.  
  18. #ifndef __MEMORY__
  19. #include <Memory.h>
  20. #endif
  21.  
  22. #ifndef __TEXTUTILS__
  23. #include <TextUtils.h>
  24. #endif
  25.  
  26.  
  27.  
  28. short myTECut(TEHandle theTE);
  29. short myTEPaste(TEHandle theTE, short* spaceBefore, short* spaceAfter);
  30. short TEIsFrontOfLine(short offset, TEHandle theTE);
  31. short TEGetLine(short offset, TEHandle theTE);
  32.  
  33.  
  34. // *****************************************************************************
  35. // *
  36. // *    myTECut()
  37. // *
  38. // *****************************************************************************
  39. short myTECut(TEHandle theTE)
  40. {    
  41.     OffsetTable    startOffsets;
  42.     OffsetTable    endOffsets;
  43.     short        selStart, selEnd, characters;
  44.     Handle        theScrap;
  45.  
  46.     if (!(characters = (**theTE).selStart - (**theTE).selEnd))
  47.         return 0;
  48.  
  49.     FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selStart,true,0L,startOffsets);
  50.     FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selEnd,false,0L,endOffsets);
  51.  
  52.     if ((startOffsets[0].offFirst == (**theTE).selStart) &&
  53.         (endOffsets[0].offSecond  == (**theTE).selEnd))
  54.         {
  55.         // both the beginning and end of the current selection is on word boundaries
  56.         selStart = (**theTE).selStart;
  57.         selEnd = (**theTE).selEnd;
  58.         if ((*((**theTE).hText))[selStart - 1] == ' ')
  59.             {
  60.             TESetSelect(selStart - 1,selEnd,theTE);
  61.             TECut(theTE);
  62.             theScrap = TEScrapHandle();
  63.             BlockMove((char*)(*theScrap) + 1,(*theScrap),TEGetScrapLength() - 1);
  64.             TESetScrapLength(TEGetScrapLength() - 1);
  65.             ZeroScrap();
  66.             TEToScrap();
  67.             characters--;
  68.             }
  69.         else
  70.             if ((*((**theTE).hText))[selEnd] == ' ')
  71.                 {
  72.                 TESetSelect(selStart,selEnd + 1,theTE);
  73.                 TECut(theTE);
  74.                 TESetScrapLength(TEGetScrapLength() - 1);
  75.                 ZeroScrap();
  76.                 TEToScrap();
  77.                 characters--;
  78.                 }
  79.             else
  80.                 TECut(theTE);
  81.         }
  82.     else
  83.         TECut(theTE);
  84.  
  85.     return characters;
  86. }
  87.  
  88.  
  89. // *****************************************************************************
  90. // *
  91. // *    myTEPaste()
  92. // *
  93. // *****************************************************************************
  94. short myTEPaste(TEHandle theTE, short* spaceBefore, short* spaceAfter)
  95. {    
  96.     OffsetTable    startOffsets;
  97.     OffsetTable    endOffsets;
  98.     short        addSpaceAfter;
  99.     short        characters;
  100.  
  101.     characters = (**theTE).selStart - (**theTE).selEnd;
  102.  
  103.     if (spaceBefore)
  104.         *spaceBefore = false;
  105.     if (spaceAfter)
  106.         *spaceAfter = false;
  107.  
  108.     FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selStart,false,0L,startOffsets);
  109.     FindWord(*((**theTE).hText),(**theTE).teLength,(**theTE).selEnd,true,0L,endOffsets);
  110.  
  111.     addSpaceAfter = ((endOffsets[0].offFirst == (**theTE).selEnd) && ((*((**theTE).hText))[(**theTE).selEnd] != ' '));
  112.  
  113.     if ((startOffsets[0].offSecond == (**theTE).selStart) && ((*((**theTE).hText))[(**theTE).selStart - 1] != ' '))
  114.         {
  115.         TEKey(' ',theTE);
  116.         characters++;
  117.         if (spaceBefore)
  118.             *spaceBefore = true;
  119.         }
  120.  
  121.     TEPaste(theTE);
  122.     characters += TEGetScrapLength();
  123.  
  124.     if (addSpaceAfter)
  125.         {
  126.         TEKey(' ',theTE);
  127.         characters++;
  128.         if (spaceAfter)
  129.             *spaceAfter = true;
  130.         }
  131.         
  132.     return characters;
  133. }
  134.  
  135.  
  136. // *****************************************************************************
  137. // *
  138. // *    TEIsFrontOfLine()
  139. // *
  140. // *****************************************************************************
  141. short TEIsFrontOfLine(short offset, TEHandle theTE)
  142. {    
  143.     short line = 0;
  144.  
  145.     if ((**theTE).teLength == 0)
  146.         return(true);
  147.  
  148.     if (offset >= (**theTE).teLength)
  149.         return ((*((**theTE).hText))[(**theTE).teLength - 1] == 0x0d);
  150.  
  151.     while ((**theTE).lineStarts[line] < offset)
  152.         line++;
  153.  
  154.     return ((**theTE).lineStarts[line] == offset);
  155. }
  156.  
  157.  
  158. // *****************************************************************************
  159. // *
  160. // *    TEGetLine()
  161. // *
  162. // *****************************************************************************
  163. short TEGetLine(short offset, TEHandle theTE)
  164. {
  165.     short line = 0;
  166.  
  167.     if (offset > (**theTE).teLength)
  168.         return((**theTE).nLines);
  169.  
  170.     while ((**theTE).lineStarts[line] < offset)
  171.         line++;
  172.     
  173.     return line;
  174. }
  175.